package com.zzy.jtxdemo;

import jodd.exception.UncheckedException;

public class WorkSession {

	static String persistedValue = "jodd";
	String sessionValue;
	boolean readOnly;
	int txno; // transaction number

	public WorkSession() { // start session in non-tx mode
	}

	public WorkSession(int txno) { // start tx session
		this.txno = txno;
	}

	public void writeValue(String value) {
		if (txno == 0) { // no transaction
			persistedValue = value;
			return;
		}
		// under transaction
		if (readOnly == true) {
			throw new UncheckedException();
		}
		sessionValue = value;
	}

	public String readValue() {
		if (sessionValue != null) {
			return sessionValue;
		}
		return persistedValue;
	}

	// commit
	public void done() {
		if (sessionValue != null) {
			persistedValue = sessionValue;
		}
		sessionValue = null;
	}

	// rollback
	public void back() {
		sessionValue = null;
	}
}